package com.yupi.oj.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yupi.oj.annotation.AuthCheck;
import com.yupi.oj.common.BaseResponse;
import com.yupi.oj.common.ResultUtils;
import com.yupi.oj.constant.UserConstant;
import com.yupi.oj.model.dto.question.QuestionAddRequest;
import com.yupi.oj.model.dto.question.QuestionEditRequest;
import com.yupi.oj.model.dto.question.QuestionPageRequest;
import com.yupi.oj.model.entity.Question;
import com.yupi.oj.model.vo.QuestionVO;
import com.yupi.oj.service.QuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yanghaifeng
 * @date 2024/4/11 15:39
 */
@RequestMapping("/question")
@RestController
@Api(tags = "题目接口")
public class QuestionController {

    @Autowired
    private QuestionService service;

    @ApiOperation("创建题目")
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> add(@RequestBody QuestionAddRequest questionAddRequest, HttpServletRequest request) {
        return ResultUtils.success(service.add(questionAddRequest, request));
    }

    @ApiOperation("删除题目")
    @GetMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> delete(@RequestParam("id") Long id, HttpServletRequest request) {
        return ResultUtils.success(service.delete(id, request));
    }

    @ApiOperation("修改题目")
    @PostMapping("/edit")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> edit(@RequestBody QuestionEditRequest questionEditRequest, HttpServletRequest request) {
        return ResultUtils.success(service.edit(questionEditRequest, request));
    }

    @ApiOperation("查询问题")
    @GetMapping("/queryById")
    public BaseResponse<QuestionVO> queryById(@RequestParam("id") Long id) {
        return ResultUtils.success(service.queryById(id));
    }

    @ApiOperation("分页查询")
    @PostMapping("/queryPage")
    public BaseResponse<Page<QuestionVO>> queryPage(Page<Question> page, @RequestBody QuestionPageRequest questionPageRequest) {
        return ResultUtils.success(service.queryPage(page, questionPageRequest));
    }

    @ApiOperation("列表查询")
    @PostMapping("/queryList")
    public BaseResponse<List<QuestionVO>> queryList(@RequestBody QuestionPageRequest questionPageRequest) {
        return ResultUtils.success(service.queryList(questionPageRequest));
    }
}
